//
//  RecommendationViewController.m
//  A段项目,漫画
//
//  Created by dlios on 15-4-28.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RecommendationViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface RecommendationViewController ()
@property(nonatomic,retain)UIScrollView *RecommScroll;
@property(nonatomic,retain)NSMutableArray *arr;
@end

@implementation RecommendationViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor purpleColor];
    
    [self creatView];
    
    [self dataPlease];
}

#pragma mark 数据解析
-(void)dataPlease
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://mobilev3.ac.qq.com/Index/commendComicListForIos/page/1/listcnt/10/clientType/0/local_version/2.3.2" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *dataArr = [dic objectForKey:@"data"];
        for (NSMutableDictionary *tempDic in dataArr) {
            Recommendation *dation = [[Recommendation alloc]init];
            [dation setValuesForKeysWithDictionary:tempDic];
            [self.arr addObject:dation];
            [dation release];
        }
//        [self.RecommScroll reloadData];
        NSLog(@"请求成功");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"数据解析失败");
        
    }];
}

//创建视图
-(void)creatView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.itemSize = CGSizeMake(WIDTH, HEIGHT);
    
    flow.minimumLineSpacing = 1;
    
    flow.minimumInteritemSpacing = 1;
    
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    UICollectionView *recommView = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen] bounds]collectionViewLayout:flow];
    recommView.dataSource = self;
    recommView.delegate = self;
    [self.view addSubview:recommView];
    
    [recommView registerClass:[RecommCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
}

#pragma mark 协议的两个方法

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RecommCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    Recommendation *dation = self.arr[indexPath.row];
    [cell.posterView sd_setImageWithURL:[NSURL URLWithString:dation.poster_cover_url]];
    return cell;
    
}

//-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    return self.arr.count;
//}
//
//-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    static NSString *reuse = @"reuse";
//    RecommendationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
//    if (cell == nil) {
//        cell = [[[RecommendationTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
//    }
//    Recommendation *dation = self.arr[indexPath.row];
//    cell.titleLable.text = dation.title;
//    cell.artistLable.text = dation.artist_name;
//    cell.gradeLable.text = dation.grade_ave;
//    cell.typeLabel.text = dation.type;
//    NSURL *url = [NSURL URLWithString:[self.arr[indexPath.row]cover_url]];
//    [cell.RecommImageView sd_setImageWithURL:url];
////    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    return cell;
//}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CartoonDViewController *cdvc = [[CartoonDViewController alloc]init];
    Recommendation *dation = self.arr[indexPath.row];
    cdvc.comic_id =dation.comic_id;
    [self.navigationController pushViewController:cdvc animated:YES];
    [cdvc release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
